<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Conditional compilation</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Conditional_compilation"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Conditional_compilation rootpage-Conditional_compilation skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Conditional compilation</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>In <a href="Computer_programming" title="Computer programming">computer programming</a>, <b>conditional compilation</b> is a <a href="Compiler" title="Compiler">compilation</a> technique which results in differing <a href="Executable_program" class="mw-redirect" title="Executable program">executable programs</a> depending on <a href="Parameter_(computer_programming)" title="Parameter (computer programming)">parameters</a> specified. This technique is commonly used when these differences in the program are needed to run it on <a href="Cross-platform" class="mw-redirect" title="Cross-platform">different platforms</a>, or with different versions of required <a href="Library_(computing)" title="Library (computing)">libraries</a> or <a href="Computer_hardware" title="Computer hardware">hardware</a>.
</p><p>Many <a href="Programming_language" title="Programming language">programming languages</a> support conditional compilation. Typically <a href="Compiler_directives" class="mw-redirect" title="Compiler directives">compiler directives</a> define or "undefine" certain <a href="Variable_(computer_science)" title="Variable (computer science)">variables</a>; other directives test these variables and modify compilation accordingly. For example, not using an actual language, the compiler may be set to define "Macintosh" and undefine "PC", and the code may contain:
</p>
<div class="mw-highlight mw-highlight-lang-pascal mw-content-ltr" dir="ltr"><pre><span class="cm">(* System generic code *)</span>
<span class="k">if</span><span class="w"> </span><span class="n">mac</span><span class="w"> </span><span class="err">!</span><span class="o">=</span><span class="w"> </span><span class="n">Null</span><span class="w"> </span><span class="k">then</span>
<span class="w"> </span><span class="cm">(* macOS specific code *)</span>
<span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">pc</span><span class="w"> </span><span class="err">!</span><span class="o">=</span><span class="w"> </span><span class="n">Null</span>
<span class="w"> </span><span class="cm">(* Windows specific code *)</span>
</pre></div>
<p>In <a href="C_(programming_language)" title="C (programming language)">C</a> and some languages with a similar syntax, this is done using an <a href="C_preprocessor#Conditional_compilation" title="C preprocessor">'#ifdef' directive</a>.
</p><p>A similar procedure, using the name "conditional comment", is used by <a href="Microsoft_Internet_Explorer" class="mw-redirect" title="Microsoft Internet Explorer">Microsoft Internet Explorer</a> from version 5 to 9 to interpret <a href="HTML" title="HTML">HTML</a> code. There is also a similar proprietary mechanism for adding conditional comments within <a href="JScript" title="JScript">JScript</a>, known as conditional compilation.<sup id="cite_ref-MS2_1-0" class="reference"><a href="#cite_note-MS2-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p><p><a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a> have preprocessor directives for conditional compilation.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span class="cp">#if DEBUG</span>
<span class="w"> </span><span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="s">"Debug version"</span><span class="p">);</span>
<span class="cp">#endif</span>
</pre></div>
<p><a href="Rust_(programming_language)" title="Rust (programming language)">Rust</a> have conditional compilation.<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-rust mw-content-ltr" dir="ltr"><pre><span class="cp">#[cfg_attr(target_os = </span><span class="s">"linux"</span><span class="cp">, path = </span><span class="s">"linux.rs"</span><span class="cp">)]</span>
<span class="cp">#[cfg_attr(windows, path = </span><span class="s">"windows.rs"</span><span class="cp">)]</span>
<span class="k">mod</span><span class="w"> </span><span class="nn">os</span><span class="p">;</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Criticism">Criticism</h2></div>
<p>When conditional compilation depends on too many variables, it can make the code harder to reason about as the number of possible combinations of configuration increases exponentially.<sup id="cite_ref-icse19_4-0" class="reference"><a href="#cite_note-icse19-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-meinicke_5-0" class="reference"><a href="#cite_note-meinicke-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-se-cc-quality_6-0" class="reference"><a href="#cite_note-se-cc-quality-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> When conditional compilation is done via a <a href="Preprocessor" title="Preprocessor">preprocessor</a> that does not guarantee syntactically correct output in the source language, such as the <a href="C_preprocessor" title="C preprocessor">C preprocessor</a>, this may lead to hard-to-debug compilation errors,<sup id="cite_ref-ifdef-harmful_7-0" class="reference"><a href="#cite_note-ifdef-harmful-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-so-avoid-ifdef_8-0" class="reference"><a href="#cite_note-so-avoid-ifdef-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-do-dont-cc_9-0" class="reference"><a href="#cite_note-do-dont-cc-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup> which is sometimes called "#ifdef hell."<sup id="cite_ref-preschern_10-0" class="reference"><a href="#cite_note-preschern-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-ifdef-hell_11-0" class="reference"><a href="#cite_note-ifdef-hell-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-MS2-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-MS2_1-0">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20080906144358/http://msdn2.microsoft.com/en-us/library/ahx1z4fs(VS.80).aspx">"Conditional Compilation"</a>. Microsoft Corporation. Archived from <a rel="nofollow" class="external text" href="http://msdn2.microsoft.com/en-us/library/ahx1z4fs(VS.80).aspx">the original</a> on 2008-09-06<span class="reference-accessdate">. Retrieved <span class="nowrap">2011-11-27</span></span>.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#conditional-compilation">"Preprocessor directives - C# reference"</a>. <i>learn.microsoft.com</i><span class="reference-accessdate">. Retrieved <span class="nowrap">31 May</span> 2025</span>.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://doc.rust-lang.org/stable/reference/conditional-compilation.html">"Conditional compilation - The Rust Reference"</a>. <i>doc.rust-lang.org</i><span class="reference-accessdate">. Retrieved <span class="nowrap">31 May</span> 2025</span>.</cite></span>
</li>
<li id="cite_note-icse19-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-icse19_4-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFGazzilloWei2019" class="citation conference cs1">Gazzillo, Paul; Wei, Shiyi (2019-05-27). <a rel="nofollow" class="external text" href="https://www.paulgazzillo.com/papers/icse19nier.pdf">"Conditional Compilation is Dead, Long Live Conditional Compilation!"</a> <span class="cs1-format">(PDF)</span>. <i>ICSE-NIER '19: Proceedings of the 41st International Conference on Software Engineering: New Ideas and Emerging Results</i>. <a rel="nofollow" class="external text" href="https://2019.icse-conferences.org/">2019 IEEE/ACM 41st International Conference on Software Engineering: New Ideas and Emerging Results (ICSE-NIER)</a>. Montreal, QC, Canada: IEEE Press. pp. <span class="nowrap">105–</span>108. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1109%2FICSE-NIER.2019.00035">10.1109/ICSE-NIER.2019.00035</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-1-7281-1758-4</bdi>. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20221107174330/https://www.paulgazzillo.com/papers/icse19nier.pdf">Archived</a> <span class="cs1-format">(PDF)</span> from the original on 2022-11-07<span class="reference-accessdate">. Retrieved <span class="nowrap">2023-01-21</span></span>.</cite></span>
</li>
<li id="cite_note-meinicke-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-meinicke_5-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFMeinickeThümSchröterBenduhn2017" class="citation book cs1">Meinicke, Jens; Thüm, Thomas; Schröter, Reimar; Benduhn, Fabian; Leich, Thomas; Saake, Gunter (2017). Meinicke, Jens; Thüm, Thomas; Schröter, Reimar; Benduhn, Fabian (eds.). <a rel="nofollow" class="external text" href="https://doi.org/10.1007/978-3-319-61443-4_12"><i>Quality Assurance for Conditional Compilation</i></a>. Cham: Springer International Publishing. pp. <span class="nowrap">131–</span>139. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1007%2F978-3-319-61443-4_12">10.1007/978-3-319-61443-4_12</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-3-319-61443-4</bdi><span class="reference-accessdate">. Retrieved <span class="nowrap">2023-01-21</span></span>.</cite> <span class="cs1-visible-error citation-comment"><code class="cs1-code">{{cite book}}</code>: </span><span class="cs1-visible-error citation-comment"><code class="cs1-code">|work=</code> ignored (help)</span></span>
</li>
<li id="cite_note-se-cc-quality-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-se-cc-quality_6-0">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://softwareengineering.stackexchange.com/questions/405980/how-does-conditional-compilation-impact-product-quality-security-and-code-compl">"compiler - How does conditional compilation impact product quality, security and code complexity?"</a>. <i>Software Engineering Stack Exchange</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2023-01-21</span></span>.</cite></span>
</li>
<li id="cite_note-ifdef-harmful-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-ifdef-harmful_7-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFLeWalkingshawErwig2011" class="citation book cs1">Le, Duc; Walkingshaw, Eric; Erwig, Martin (2011-09-18). "#ifdef confirmed harmful: Promoting understandable software variation". <a rel="nofollow" class="external text" href="https://ieeexplore.ieee.org/document/6070391"><i>2011 IEEE Symposium on Visual Languages and Human-Centric Computing (VL/HCC)</i></a>. pp. <span class="nowrap">143–</span>150. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1109%2FVLHCC.2011.6070391">10.1109/VLHCC.2011.6070391</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-1-4577-1246-3</bdi>.</cite></span>
</li>
<li id="cite_note-so-avoid-ifdef-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-so-avoid-ifdef_8-0">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://stackoverflow.com/questions/1851181/why-should-ifdef-be-avoided-in-c-files">"conditional compilation - Why should #ifdef be avoided in .c files?"</a>. <i>Stack Overflow</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2023-01-21</span></span>.</cite></span>
</li>
<li id="cite_note-do-dont-cc-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-do-dont-cc_9-0">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://stackoverflow.com/questions/4469645/dos-and-donts-of-conditional-compile">"c++ - Dos and Don'ts of Conditional Compile"</a>. <i>Stack Overflow</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2023-01-21</span></span>.</cite></span>
</li>
<li id="cite_note-preschern-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-preschern_10-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFPreschern2019" class="citation conference cs1">Preschern, Christopher (2019-07-03). "Patterns to escape the #ifdef hell". <a rel="nofollow" class="external text" href="http://preschern.azurewebsites.net/2019-os-specific-code.pdf"><i>Proceedings of the 24th European Conference on Pattern Languages of Programs</i></a> <span class="cs1-format">(PDF)</span>. New York, NY, USA: Association for Computing Machinery. pp. <span class="nowrap">1–</span>12. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1145%2F3361149.3361151">10.1145/3361149.3361151</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-1-4503-6206-1</bdi>. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20221221151021/http://preschern.azurewebsites.net/2019-os-specific-code.pdf">Archived</a> <span class="cs1-format">(PDF)</span> from the original on 2022-12-21.</cite></span>
</li>
<li id="cite_note-ifdef-hell-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-ifdef-hell_11-0">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.cqse.eu/en/news/blog/living-in-the-ifdef-hell/">"Living in the #ifdef Hell"</a>. <i>www.cqse.eu</i>. 28 October 2015. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20221128024445/https://www.cqse.eu/en/news/blog/living-in-the-ifdef-hell/">Archived</a> from the original on 2022-11-28<span class="reference-accessdate">. Retrieved <span class="nowrap">2023-01-21</span></span>.</cite></span>
</li>
</ol></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-06-19" href="https://en.wikipedia.org/wiki/?title=Conditional_compilation&oldid=1296317913">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>